package com.ruoyi.web.controller.durian.mq;

import com.rabbitmq.client.Channel;
import com.ruoyi.common.config.RabbitConfig;
import com.ruoyi.framework.websocket.WebSocketUsers;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @Description RabbitMQ 定时消息队列 消费监听回调
 **/
@Component
public class RabbitConsumer {

    @RabbitListener(queues = RabbitConfig.msg_queue) //监听队列
    public void msgReceive(String content, Message message, Channel channel) throws IOException {
        // 发送给WebSocket 由WebSocket推送给前端
        String[] value = content.split("#SPLIT#");
        WebSocketUsers.sendMessageById(value[0], value[1]);
        // ask手动确认消息已接收
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }
}

